//
//  GHVerifyViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/16.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHVerifyViewController.h"
#import "STSendButton.h"
#import "GHSetPwdViewController.h"
@interface GHVerifyViewController ()<STSendButtonDlegate>
@property(nonatomic, strong) STSendButton                     *sendButton;
@property(nonatomic, strong) UITextField                     *codeTextFiled;
@end

@implementation GHVerifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"身份验证";
    [self configSubView];
    // Do any additional setup after loading the view.
}
- (void)dealloc{
    [self.sendButton timerEnd];
    self.sendButton= nil;
    
}
#pragma mark --subView
- (void)configSubView{
    [self configTableHeader];
    [self configTableFooter];
}
- (void)configTableHeader{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor =  [UIColor whiteColor];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 25, UIScreenWidth, 20)
                                                     text:@"当前手机号码138*****1884"
                                                textColor:GH_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [header addSubview:titleLable];
    
    self.codeTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, titleLable.st_bottom + 40, 200, 13)];
    self.codeTextFiled.placeholder = @"请输入验证码";
    self.codeTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    self.codeTextFiled.textColor= GH_secendTextColor;
    [header addSubview:self.codeTextFiled];
    
    self.sendButton = [[STSendButton alloc] initWithFrame:CGRectMake(0, 0, 80, 15) andWithDuration:60];
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.sendButton setTitleColor:GH_YellowBackGroundColor forState:UIControlStateNormal];
    [self.sendButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.sendButton.backgroundColor = [UIColor clearColor];
    self.sendButton.delegate = self;
    self.sendButton.st_right = UIScreenWidth -  10;
    self.sendButton.st_centerY = self.codeTextFiled.st_centerY;
    [header addSubview:self.sendButton];
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(10, self.codeTextFiled.st_bottom + 10, UIScreenWidth - 20, 0.5)];
    line.backgroundColor = GH_lineColor;
    [header addSubview:line];
    
    
    header.st_height = line.st_bottom + 15;
    self.tableView.tableHeaderView = header;
}
- (void)configTableFooter{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * nextButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:@"下一步"
                                                   titleColor:GH_YellowBackGroundColor
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:GH_BlackBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [nextButton setClicAction:^(UIButton *sender) {
        [weakSelf.navigationController pushViewController:[GHSetPwdViewController new] animated:YES];
    }];
    nextButton.st_centerY = 50;
    nextButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:nextButton];
    self.tableView.tableFooterView = footer;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 0    ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 11;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 1;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    cell.textLabel.text = [NSString stringWithFormat:@"%lu",indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
#pragma mark --STSendButtonDlegate
- (BOOL)STSendButtonWillClic:(UIButton *)button{

    return YES;
}
- (void)STSendButtonDidClic:(UIButton*)button isFirstClic:(BOOL)isFirstClic duration:(NSInteger)duration{
    
    
    if (isFirstClic) {
        [self sendcodeRequest];
    }
}

- (void)STSendButtonDidCountdown:(UIButton*)button duration:(NSInteger)duration{
    [self.sendButton setTitle:[NSString stringWithFormat:@"%ld秒",duration] forState:UIControlStateNormal];
}
- (void)STSendButtonTimeEnded:(UIButton*)button{
    [self.sendButton timerEnd];
    [self.sendButton setTitle:@"获取验证码" forState:UIControlStateNormal];
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)sendcodeRequest{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if (1) {
        [dic setObject:@"18380461887" forKey:@"mobile"];
    }
    [SVProgressHUD showWithStatus:@"正在发送"];
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/User/sendSms"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"验证码已成功发送"];
                                                 [self.sendButton timerBegin];
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [self.sendButton timerEnd];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

@end
